
#include<vector>
#include<algorithm>
#include<stack>
#include <climits> 
struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;

    TreeNode() : val(0), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x) : val(x), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right)
    {
    }
};

class Solution {
public:
    bool isB = true;
    std::stack<TreeNode*> st;
    long last = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        isB= true;
if (!root) return true;
        TreeNode* p = root;
        while(!st.empty()||p!=nullptr)
        {
           if (p!=nullptr)
           {
               st.push(p);
               p=p->left;
               
           }
            else
            {
                p = st.top();
                st.pop();
                if (p->val <= last) return false;
                last = p->val;
                p = p->right;
            }
           
        }
        return true;
    }
};

